# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class ControlpanelController < ApplicationController
  # Following tells that all the Control panel pages 
  # should use no_sub_tabs.rhtml template which doesnt
  # have subtabs.
  layout 'no_sub_tabs'
  
  def list
     @config = YAML::load(File.open("#{RAILS_ROOT}/config/appconfig/ajubyconfig.yml" )) 
     #puts YAML::dump(@config)
  end
  
  def appconfig
    @config = YAML::load(File.open("#{RAILS_ROOT}/config/appconfig/ajubyconfig.yml" )) 
    @op = params['op']
    return if @op==nil
    @config.each_key {|key| 
        config_val = params[key]
        if config_val!=nil
          @config[key]['value'] = config_val
          AJUBY_CONFIG[key]['value']=config_val
        end  
     }
    
    begin 
      File.open("#{RAILS_ROOT}/config/appconfig/ajubyconfig.yml", 'w' )  do |f| 
        f << @config.to_yaml 
      end
      #require ('config/appconfig/init')
     rescue
      flash[:warning] = "Applications settings update failed!."
    end  
    flash[:simplemsg] = "Applications settings updated successfully."
    
   end 
 
 # ------------- CODE FOR THEME EDITOR ------------------------------

def get_theme_root

  current_directory=Dir.getwd
  current_directory=current_directory.concat("/themes")
  return current_directory
       
end

def get_all_themes

  $theme_array=Array.new()
  i=0
  j=0
  k=0
  subdirectory_count=0

  $theme_root=get_theme_root()
  themes_dir=Dir.new($theme_root)

  for theme_name in themes_dir
    if(!theme_name.index("."))
      $theme_array[i]=theme_name
      i=i+1
    end
  end

  # To get the no of themes
  theme_array_count=$theme_array.length 

  # Taking the subdirectory count under themes
  
  for j in 0..theme_array_count-1
    subdir_path=$theme_root + "/" + $theme_array[j]
    subdir=Dir.new(subdir_path)
    for subdir_name in subdir
      if(!subdir_name.index(".") and subdir_name!= "images")           
        subdirectory_count=subdirectory_count+1
      end
    end
  end

  # Storing an associative array for themes and its subdirectories

  $subdirectory_files=Array.new(subdirectory_count) { Hash.new }
  
  for j in 0..theme_array_count-1
    subdir_path=$theme_root + "/" + $theme_array[j]
    subdir=Dir.new(subdir_path)
    for subdir_name in subdir
      if(!subdir_name.index(".") and subdir_name!= "images")           
        $subdirectory_files[k][$theme_array[j]] = subdir_name
        k=k+1
      end
    end  
  end
      
end


def get_subdirectories
  
  $subdirectory_link=""
  $current_theme=params[:list]
  $temp_subdir=Array.new()
  k=0
  get_all_themes()

  subdirectory_array_count=$subdirectory_files.length

  # Taking the subdirectories under the chosen theme from the option box
  
  for index in 0..subdirectory_array_count-1 
    if($subdirectory_files[index][$current_theme]!= nil)
        $temp_subdir[k]=$subdirectory_files[index][$current_theme]
         $subdirectory_link=$subdirectory_link+"<b><%=link_to_remote '"+$subdirectory_files[index][$current_theme]+"', :update => \"filenames\", :url => { :action => \"get_files\", :id => '"+$subdirectory_files[index][$current_theme]+"'} %>"+"<br>"
         k=k+1
    end
  end
  
  temp_arr_count=$temp_subdir.length
  render :inline => $subdirectory_link
  
end


def get_files
  
  $clicked_link=params[:id]
  $file_names=Array.new()
  $file_names_link=""
  j=0
  temp_subdir_count=$temp_subdir.length
  
  # Taking all the files based on the selected subdirectory
  
  for index in 0..temp_subdir_count-1
    if($clicked_link == $temp_subdir[index])
       file_path=$theme_root + "/" + $current_theme + "/" + $temp_subdir[index]        
       file_dir=Dir.new(file_path)
         
        for file_name in file_dir
          if(!file_name.match(".svn") and file_name!= "." and file_name!= "..")
            $file_names[j]=file_name
            $file_names_link=$file_names_link+"<b><%=link_to_remote '"+file_name+"' , :update=>\"body\", :url => { :action => \"get_file_data\", :id => '"+file_name+"'} %>"+"<br>"
            j=j+1
          end
        end 
    end
  end

  render :inline => $file_names_link
end


def get_file_data

  $clicked_file=params[:id]
  $file_data=""
  $str=""
  
  file_name_array_length=$file_names.length

  # Taking the file content based on the chosen file
  
    for index in 0..file_name_array_length
      if($clicked_file == $file_names[index])
        $file_path=$theme_root + "/" + $current_theme + "/" + $clicked_link + "/" + $clicked_file                
          File.open($file_path,"r") do |file|
            file.each_line { |data| 
            $file_data = $file_data + data
            }
          end  
      end
    end
       
    render :inline => $file_data
end


def update_file

  content=""
  content=params[:body]

  # Overwriting the file.

  if(File.writable?($file_path))
      f= File.new($file_path,"w+")
      f.write(content)
      f.close
    end
 
    redirect_to :action =>'themeeditor' 
end

def home
end


end

